const sqlExecute = require("../../database/activity/query")
const userExecute = require("../../database/user/query")
const dayjs = require('dayjs')

// 获取优惠券列表
const getPageList = async (req, res) => {
  const page = req.params.page
  const limit = req.params.limit

  // 分页查询
  const sql = `SELECT * FROM coupon_info  where is_deleted = 0 LIMIT ${(page - 1) * limit}, ${limit}`

  const sqlCount = 'select count(*) as count from coupon_info where is_deleted = 0'
  const result = await sqlExecute(sql)
  const resultCount = await sqlExecute(sqlCount)

  res.send({
    status: 1,
    code: 20000,
    data: {
      records: result,
      total: resultCount[0].count,
    }
  })
}

// 根据id获取优惠券
const getById = async (req, res) => {
  const id = req.params.id

  const sql = `SELECT * FROM coupon_info where id = ${id} and is_deleted = 0`

  const result = await sqlExecute(sql)

  res.send({
    status: 1,
    code: 20000,
    data: result[0]
  })
}

// 更新优惠
const updateById = async (req, res) => {
  const couponInfo = req.body
  // 更新时间
  couponInfo.update_time = new Date()
  couponInfo.start_time = dayjs(couponInfo.start_time).format('YYYY-MM-DD HH:mm:ss')
  couponInfo.end_time = dayjs(couponInfo.end_time).format('YYYY-MM-DD HH:mm:ss')
  couponInfo.expire_time = dayjs(couponInfo.expire_time).format('YYYY-MM-DD HH:mm:ss')

  // 删除一些字段
  delete couponInfo.create_time

  const sql = `UPDATE coupon_info SET ? WHERE id = ${couponInfo.id}`

  const result = await sqlExecute(sql, couponInfo)

  res.send({
    status: 1,
    code: 20000,
    message: '更新成功',
    data: result
  })
}

// 新增优惠
const add = async (req, res) => {
  const couponInfo = req.body
  // 更新时间
  couponInfo.create_time = new Date()
  couponInfo.start_time = dayjs(couponInfo.start_time).format('YYYY-MM-DD HH:mm:ss')
  couponInfo.end_time = dayjs(couponInfo.end_time).format('YYYY-MM-DD HH:mm:ss')
  couponInfo.expire_time = dayjs(couponInfo.expire_time).format('YYYY-MM-DD HH:mm:ss')

  // 删除一些字段
  delete couponInfo.id

  // 过滤空字段
  const couponInfoFilter = { }
  for (let key in couponInfo) {
    if (couponInfo[key] !== '') {
      couponInfoFilter[key] = couponInfo[key]
    }
  }

  const sql = `INSERT INTO coupon_info SET ?`

  const result = await sqlExecute(sql, couponInfoFilter)

  res.send({
    status: 1,
    code: 20000,
    message: '新增成功',
    data: result
  })
}

// 删除优惠
const removeById = async (req, res) => {
  const id = req.params.id

  const sql = `UPDATE coupon_info SET is_deleted = 1 WHERE id = ${id}`

  const result = await sqlExecute(sql)

  res.send({
    status: 1,
    code: 20000,
    message: '删除成功',
    data: result
  })
}

// 获取优惠券使用列表
const getPageCouponUseList = async (req, res) => {
  const page = req.params.page
  const limit = req.params.limit
  const id = req.query.coupon_id

  console.log(id)
  // 分页查询
  const sql = `SELECT * FROM coupon_use  where is_deleted = 0 and coupon_id=${id} LIMIT ${(page - 1) * limit}, ${limit}`
  const result = await sqlExecute(sql)
  // 获取总数
  const sqlCount = 'select count(*) as count from coupon_use where is_deleted = 0 and coupon_id=' + id
  const resultCount = await sqlExecute(sqlCount)

  // 获取用户信息
  for (let item of result) {
    const userSql = `SELECT * FROM user_info where is_deleted = 0 and id = ${item.user_id}`
    const userResult = await userExecute(userSql)
    item.param = userResult[0]
  }

  res.send({
    status: 1,
    code: 20000,
    data: {
      records: result,
      total: resultCount[0].count,
    }
  })
}

module.exports = {
  getPageList,
  getById,
  updateById,
  add,
  removeById,
  getPageCouponUseList,

}
